package com.zxk28.notebook;

import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class SearchDialog {
	
	private static ResourceBundle res = ResourceBundle.getBundle("notebook");
	
	private Shell shell = null;
	
	private Combo keyCombo = null;
	private Button upButton = null;
	private Button downButton = null;
	private Button caseButton = null;
	private Button findButton = null;
	
	private String key = null;
	private boolean caseSensitive = false;
	private int direction = SWT.DOWN;
	private int userSelect = SWT.CANCEL;
	
	public SearchDialog(Shell parent) {
		shell = new Shell(parent, SWT.TITLE | SWT.BORDER | SWT.CLOSE);
		shell.setLayout(new GridLayout());
		shell.addShellListener(new ShellAdapter() {
			@Override
			public void shellClosed(ShellEvent e) {
				e.doit = false;
				shell.setVisible(false);
			}
		});
		
		createUI();
	}
	
	private void createUI() {

		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		composite.setLayout(new GridLayout(2, false));
		
		Label label = new Label(composite, SWT.RIGHT);
		label.setText(res.getString("Window.Dialog.Find"));
		keyCombo = new Combo(composite, SWT.DROP_DOWN);
		GridData data = new GridData(SWT.FILL, 0, true, false);
		data.widthHint = 140;
		keyCombo.setLayoutData(data);
		keyCombo.addModifyListener(new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e) {
				key = keyCombo.getText();
			}
		});
		
		Group group = new Group(shell, SWT.SHADOW_ETCHED_IN);
		group.setLayoutData(new GridData(SWT.FILL, 0, true, false));
		group.setLayout(new GridLayout(2, true));
		group.setText(res.getString("Window.Dialog.Diretion"));
		upButton = new Button(group, SWT.RADIO);
		upButton.setText(res.getString("Window.Dialog.UP"));
		upButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				direction = upButton.getSelection() ? SWT.UP : SWT.DOWN;
			}
		});
		
		downButton = new Button(group, SWT.RADIO);
		downButton.setText(res.getString("Window.Dialog.Down"));
		downButton.setSelection(true); // default select
		downButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				direction = downButton.getSelection() ? SWT.DOWN : SWT.UP;
			}
		});
		
		caseButton = new Button(shell, SWT.CHECK);
		caseButton.setText(res.getString("Windwow.Dialog.Case"));
		caseButton.setLayoutData(new GridData(SWT.FILL, 0, true, false));
		caseButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				caseSensitive = caseButton.getSelection();
			}
		});
		
		composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, 0, true, false));
		composite.setLayout(new GridLayout());
		((GridData)composite.getLayoutData()).horizontalAlignment = SWT.RIGHT;
		
		findButton = new Button(composite, SWT.PUSH);
		findButton.setLayoutData(new GridData(80, SWT.DEFAULT));
		findButton.setText(res.getString("Window.Dialog.Find"));
		findButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				userSelect = SWT.OK;
				if(key != null) {
					if(keyCombo.getItems().length == 0)
						keyCombo.add(key);
					for (String item : keyCombo.getItems()) {
						if(!item.equals(key)) {
							keyCombo.add(key);
							break;
						}
					}
				}
				shell.setVisible(false);
			}
		});
		
		shell.pack();
	}
	
	public int open() {
		
		userSelect = SWT.CANCEL;

		shell.open();

		return userSelect;
	}
	
	public String getKey() {
		return key;
	}
	
	public boolean isCaseSensitive() {
		return caseSensitive;
	}
	
	public int getDirection() {
		return direction;
	}
	
}
